var indexPage = {

    init: function() {
        var url = location.href;
        if (url.indexOf('/resources/theme/html/index.html') != -1) {
            MZ_stat && MZ_stat.report('load_online_edit');
        }
        this.bindEvents();
    },

    bindEvents: function() {
        var self = this;

        $("#create").click(function() {
            $("#select").hide();
            $("#creation").show();
            $("#themeName").val("").focus();
            $("#enter").show();
            $("#enterImport").parents("form").hide();
        });

        $("#versionUL li a").click(function() {
            var val = $(this).attr("data-value");
            val ? $('#resolutionDL').show() : $('#resolutionDL').hide();
            $("[data-name='resolution']").attr("data-value", val).text(val)
            $(":button[data-name='system']").attr("data-value", $(this).text());
        });

        $("#themeName").keyup(function(e) {
            var len = $(this).val().length;

            $("#textLen").text(len);
            if (len > 0 && len <= 30) {
                $("#enter").removeClass("enter_disable").removeClass("enter-disable");
                $("#enterImport").parents("form").removeClass("enter_disable").removeClass("enter-disable");
            } else if (!$("#enter").hasClass("enter_disable") && !$("#enter").hasClass("enter-disable")) {
                $("#enter").addClass("enter_disable").addClass("enter-disable");
                $("#enterImport").parents("form").addClass("enter_disable").addClass("enter-disable");
            }

            if (e.which == 13 && $.trim($(this).val())) {
                $("#enter").trigger("click");
            }
        });

        $("#enter").click(function() {
            if ($("#enter").hasClass("enter_disable") || $("#enter").hasClass("enter-disable")) {
                return;
            }
            themeDB.closeDB();
            var serialize = new util.serialize;
            var json = serialize.json();
            var src = "";
            var config = null;
            var maker = "";
            if (json.system == "Flyme4") {
                src = "//designer.meizu.com/resources/theme/js/f4-1920x1152.js?v=20150427";
            } else if (json.system == "Flyme5") {
                src = "//designer.meizu.com/resources/theme/js/Flyme5/f5.js";
            } else if (json.system == "Flyme6") {
                src = "//designer.meizu.com/resources/theme/js/Flyme6/f6.js";
            } else if (json.system == "Flyme7") {
                src = "//designer.meizu.com/resources/theme/js/Flyme7/f7.js";
            } else if (json.system == "Flyme8") {
                src = "//designer.meizu.com/resources/theme/js/Flyme8/f8.js";
            }
            util.createScript({
                id: "theme" + json.resolution,
                src: src
            }, function() {
                if (json.system == "Flyme3.0") {
                    config = initConfig_f3_1800x1080;
                    maker = "maker-f3-1800x1080";
                } else if (json.system == "Flyme4") {
                    config = initConfig_f4_1920x1152;
                    maker = "maker-f4";
                } else if (json.system == "Flyme5") {
                    config = initConfig_f5;
                    maker = "maker-f5";
                } else if (json.system == "Flyme6") {
                    config = initConfig_f6;
                    maker = "maker-f6";
                } else if (json.system == "Flyme7") {
                    config = initConfig_f7;
                    maker = "maker-f7";
                } else if (json.system == "Flyme8") {
                    config = initConfig_f8;
                    maker = "maker-f8";
                }
                var param = {
                    config: "config",
                    initConfig: config
                };
                // window.location.href = "/resources/theme/html/"+ maker +".html?themeName="+ encodeURI(json.themeName);
                themeDB.setItem(param, function(e) {
                    param.initConfig.id = "initConfig";
                    themeDB.setItem(param, function() {
                        window.location.href = "/resources/theme/html/" + maker + ".html?themeName=" + encodeURI(json.themeName);
                    });
                });
            });
        });

        $("#back").click(function() {
            $("#creation").hide();
            $("#select").show();
        });

        $("#login").click(function() {
            location = 'https://member.meizu.com/login/login.html' +
                '?service=designer&appuri=http://designer.meizu.com/login&useruri=' + encodeURIComponent(window.location.href);
        });
        $("#logout").click(function() {
            location = 'https://member.meizu.com/logout.jsp?useruri=' + encodeURIComponent(window.location.href);
        });
    }
};

$(function() {
    indexPage.init();
    util.checkLogin();
    if (!$.browser.webkit) {
        $("#select").remove();
        //        $("#browseModal").modal({ keyboard:false, backdrop: "static"}).modal("show");
        util.alert('在线工具暂只支持Chrome浏览器', function() {
            window.open("http://www.google.cn/intl/zh-CN/chrome/browser/", "newwindow");
            return false;
        }, '下载Chrome浏览器');
    }

    $('#import').trigger('click');
});